<--- %%NOBANNER%% --> crfile.sas
 BackForward

/*-------------------<---Start of Description-->---------------------\
| Translate an entire library of datasets into output files; one file|
| per dataset with the same names as the dataset names;              |
|---------------------<---End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<---Start of Files or Arguments Needed-->---------------|
| Parameters:                                                        |
|    libname - the dataset library name need to be translated;       |
|    outputdir - the directory you want to save the output files;    |
|-------------<---End of Files or Arguments Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<---Start of Files Created-->---------------------|
| Example:%crfile(libname=acedcrf, outputdir=d:\temp\, filetype=doc);|
| Usage:  %crfile(libname=, outputdir=);                             |
\-------------------<---End of Files Created-->---------------------*/
%macro crfile(libname=, outputdir=, filetype=xls);
/*-------------------------------------------\
| Author:  Duo Zhou;                         |
| Created: 1-1-2002 11:20pm;                 |
| Purpose: List all datasets under the       |
|          library reference;                |
\-------------------------------------------*/
%local num i ndsns dsn filetype fileext _tmplast_;
%let _tmplast_=&syslast;
%let libname=%sysfunc(dequote(&libname));
%let outputdir=%sysfunc(dequote(&outputdir));
%if (%length(%trim(%left(&outputdir))) >1) %then %do;
   %if (%substr(&outputdir, %length(&outputdir), 1) ne %str(\)) %then %do;
      %let outputdir=&outputdir.\;
   %end;
   %put outputdir is &outputdir.;
%end;
%if (%index(%quote(%upcase(&filetype)),%quote(XLS))) %then %do;
   %let filetype=EXCEL;
   %let fileext=.xls;
%end;
%else %if (%index(%quote(%upcase(&filetype)),%quote(DOC))) %then %do;
   %let filetype=WORD;
   %let fileext=.doc;
%end;
%else %if (%index(%quote(%upcase(&filetype)),%quote(HTML))) %then %do;
   %let filetype=HTML;
   %let fileext=.html;
%end;
proc datasets library=&libname memtype=data;
   contents out=work._temp1(keep=memname engine nobs varnum name sorted sortedby nodupkey noduprec) data=_all_ noprint;
run;
proc sort data=_temp1; by memname sorted sortedby; run;
%if %nobs(_temp1)>0 %then %do;
   /*** Cocatenate data set names ***/
   proc sql noprint;
     select distinct memname
     into :datanames separated by ' '
     from _temp1
     where name ne ' ';
   quit;%put datanames is &datanames.;
   /*** Create excel files ***/
   %let ndsns=%words(&datanames);
   %put There are &ndsns data involved.;
   ods listing close;
   %do i=1 %to &ndsns;
      %let filename=;
      %let dsn&i=%qscan(&datanames, &i, %str( ));
      %let filename=&&dsn&i..&fileext;
      %put SAS are writing to the &i.th file: "&outputdir.&&dsn&i..&fileext".;
      ods html file="&outputdir.&filename"
               style=duostyle;
      proc print data=&libname..&&dsn&i label;
      title "&&dsn&i";
      run;
      ods html close;
   %end;
   ods listing;
   proc datasets library=work nolist;
     delete _temp1;
   run;quit;
%end;
%else %put There is no datasets in the library "&libname".;
%let syslast=&_tmplast_;
%mend crfile;